'use strict';

const Controller = require('../core/base_controller');

class User extends Controller {

	async login() {

		const { app, ctx } = this;

		const {username, password} = ctx.request.body;

		if(username == undefined || password == undefined){
			this.error(10001, '请输入用户名密码')
		}

		const user = await ctx.service.user.login(username, password);

		console.log(user);

		if(user == undefined){
            this.error(10002, '用户名密码错误');
            return;
        }

        const role = await ctx.service.role.getListByUserid(user.id);

        const uuid = await ctx.service.util.uuid(40, 16);

        console.log(uuid);

        ctx.cookies.set('dlqtoken', uuid);

        const reidsValue = {
        	_user : user,
        	_role : role,
        };

        console.log(reidsValue);

        await app.redis.get('member').set('dlq_session' + uuid + '_info', JSON.stringify(reidsValue));
        await app.redis.get('member').expire('dlq_session' + uuid + '_info', 24 * 3600);

		this.success(uuid);
	}


	async list () {

		const result = await this.ctx.service.user.list();

		this.success(result);
	}

	async update () {

		const { ctx } = this;

		const result = await ctx.service.user.update(ctx.request.body);

		this.success(result);
	}

	async info () {

		this.success('22211111');
	}
}

module.exports = User;
